﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinnerCMS.BLL;
using WinnerCMS.CacheStorager;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    public class MenuManage
    {
        private static readonly MemuDal MyDAL = new MemuDal();

        /// <summary>
        /// 菜单的完整数据
        /// </summary>
        /// <returns></returns>
        private IList<Memu> GetList()
        {
            var memulist = MyDAL.GetList();

            foreach (var x in memulist)
            {
                if (string.IsNullOrEmpty(x.Link) || x.ModelId == 0)
                {
                    continue;
                }

                if (x.Link.Contains("?") == false)
                {
                    x.Link = x.Link + "?ModelId=" + x.ModelId;
                }
                else
                {
                    x.Link = x.Link + "&ModelId=" + x.ModelId;
                }
            }


            var mlist = new ModelManage().GetList();
            mlist.ForEach(x => memulist.Add(new Memu
            {
                Id = Guid.NewGuid().ToString(),
                Pid = "AE630B27-953A-4E2B-B186-0D2E41598E65",
                Link = "ContentRelease/MakeHtml.aspx?ModelId=" + x.Id,
                Title = "生成" + x.ModelName
            }));
            return memulist;
        }

        public IList<Memu> GetList(string pid)
        {
            IList<Memu> list = GetList();

            return list.Where(x => x.Pid == pid).ToList();
        }

        #region 树形数据源

        public IList<TreeNode> GetTreeData(string pid)
        {
            return GetTreeData(pid, "");
        }

        public IList<TreeNode> GetTreeData(string pid, string type)
        {
            List<Memu> olist;
            if (type == "CategoryTree")
            {
                olist = GetCategoryMemu(); //加塞栏目分类
                if (!olist.Any())
                {
                    olist.Add(new Memu
                    {
                        Id = "-9999",
                        Title = "没有栏目，可能是没有栏目或者没有权限。",
                        Pid = "FED2B701-7295-47D9-8187-710D873463EA",
                        Link = "Category/Manage.aspx"
                    });
                }

                var templist = GetList();
                templist.ForEach(x => x.OrderId = x.OrderId + 999);
                olist.AddRange(templist);
            }
            else
            {
                olist = GetList().ToList();
            }

            pid = pid ?? string.Empty;

            var newlist = new List<TreeNode>();
            var arr = (from x in olist
                       where x.Pid == pid
                       orderby x.OrderId
                       select TreeNode.ToTreeNode(x)).ToList();


            newlist.AddRange(arr);

            foreach (var n in newlist)
            {
                BuildingTreeData(olist, newlist, n, n.Id);
            }


            return newlist;
        }

        /// <summary>
        /// 建立数据
        /// </summary>
        /// <param name="olist"></param>
        /// <param name="newlist"></param>
        /// <param name="n"></param>
        /// <param name="pid"></param>
        private void BuildingTreeData(IEnumerable<Memu> olist, IList<TreeNode> newlist, TreeNode n, string pid)
        {
            var arr = (from x in olist
                       where x.Pid == pid
                       orderby x.OrderId
                       select TreeNode.ToTreeNode(x)).ToArray();

            n.Children.AddRange(arr);

            foreach (var a in n.Children)
            {
                BuildingTreeData(olist, newlist, a, a.Id);
            }
        }

        #endregion

        public void ClearCache()
        {
            CMSCaches.Context.RemoveLike("WinnerCMS_Menu_ZPid");
        }

        private List<Memu> GetCategoryMemu()
        {
            IList<Category> clist = new CategoryManage().GetCategoryList();

            List<Memu> memulist = new List<Memu>();
            memulist.AddRange(clist.Select(c => new Memu
            {
                Id = c.Id.ToString(),
                Pid = c.ParentId == 0 ? "FED2B701-7295-47D9-8187-710D873463EA" : c.ParentId.ToString(),
                Link = "Common/ContentManage.aspx?ModelId=" + c.ModelId + "&CategoryId=" + c.Id,
                Title = c.CategoryName,
                OrderId = c.OrderId
            }));


            return memulist;
        }
    }

    /// <summary>
    /// 自定义的筛选器
    /// </summary>
    /// <remarks>自定义为 like 'ABC%' 和 in 的综合效果</remarks>
    internal class MenuEqualityComparer : IEqualityComparer<string>
    {
        public bool Equals(string b1, string b2)
        {
            return b1.EndsWith(b2);
        }


        public int GetHashCode(string obj)
        {
            throw new NotImplementedException();
        }
    }
}